<template>
    <div>
        <div>
          <h2 style="text-align: left;display: inline-block;width: 50%">全部直播</h2>
          <div style="display: inline-block;width: 50%;text-align: right">
            <el-button type="primary" @click="show">领取优惠券</el-button>
          </div>
        </div>
        <div>
          <el-row :gutter="12">
            <el-col :span="6" v-for="s of liveList">
              <div @click="goPage3(s)" class="cards" style="border-radius: 10%;margin-top: 10px;cursor: pointer;background-color: white;padding: 10px" >
                <img :src="nginxUrl+s.livecover" style="height: 100px; width: 100px;border-radius: 50%" >
                <p v-text="s.livename"></p>
              </div>
            </el-col>
          </el-row>
        </div>


      <el-dialog
          title="请选择优惠券"
          :visible.sync="dialogVisible"
          width="30%">
        <span>
          <el-select v-model="couponId" placeholder="请选择">
            <el-option
                v-for="item in couponList"
                :key="item.id"
                :label="item.couponname"
                :value="item.id">
            </el-option>
          </el-select>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="cancle">取 消</el-button>
          <el-button type="primary" @click="sure">确 定</el-button>
        </span>
      </el-dialog>

    </div>
</template>

<script>
    export default {
      name: "WelCome",

      data() {
        return {
          nginxUrl:this.$MyUrl.imgUrl+"course/",
          liveList:[],
          couponList:[],
          dialogVisible: false,
          userId:10,
          couponId:'',
          couponReport:{
            userId:'',
            couponId:''
          },
          liveId:'',
          liveData:'',
          userBehavior:{},
          user:{}

        }
      },

      methods:{
        goPage3(row){
          this.liveId=row.id
          let url = "liveDateVo/queryByLiveId"
          this.$axios.get(url, {
            params:{
              liveId:this.liveId
            }
          })
              .then(res=>{
                if (res.data.code == 101){
                  this.liveData = res.data.data
                  let url = "liveDateVo/update"
                  this.$axios.put(url,this.liveData)
                      .then(res=>{
                        if (res.data.code == 101){
                          this.doUserBehavior(row)





                        }else{
                          this.$message({
                            type:"error",
                            message:res.data.msg,
                            duration:1000
                          })
                        }
                      })

                }else if (res.data.code == 102){
                  let url = "liveDataVo/add"
                  this.$axios.post(url,this.liveId)
                      .then(res=>{
                        if (res.data.code == 101){
                          this.doUserBehavior(row)



                        }else{
                          this.$message({
                            type:"error",
                            message:res.data.msg,
                            duration:1000
                          })
                        }
                      })
                }else{
                  this.$message({
                    type:"error",
                    message:res.data.msg,
                    duration:1000
                  })
                }
              })
        },
        pushPage(row){
          this.$router.push({
            path: "/page3/",
            query: {
              liveId: row.id,
              userId:this.userId
            }
          })
        },
        doUserBehavior(live){
          let url = "userBehavior/queryByUserId"
          this.$axios.get(url,{
            params:{
              userId:this.userId
            }
          }).then(res=>{
            if (res.data.code == 101){
              this.userBehavior = res.data.data
              let now = new Date
              let y = now.getFullYear()
              let m = now.getMonth() +1
              let d = now.getDate()
              let date = new Date(y+"-"+m+"-"+d)
              let date2 = this.userBehavior.studyTime
              let url = "userBehavior/update"
              let studyTime = new Date(date2)

              if (date - studyTime == 0){
                this.userBehavior.learningTimeToday = this.userBehavior.learningTimeToday + live.liveduration
                this.userBehavior.learningTime = this.userBehavior.learningTime + live.liveduration
                this.userBehavior.learningCourseQuantity = this.userBehavior.learningCourseQuantity + 1
              }else{
                this.userBehavior.learningTimeToday =  live.liveduration
                this.userBehavior.learningTime = this.userBehavior.learningTime + live.liveduration
                this.userBehavior.learningCourseQuantity = this.userBehavior.learningCourseQuantity + 1
              }
              this.$axios.put(url,this.userBehavior)
                  .then(res=>{
                    if (res.data.code == 101){
                      this.pushPage(live)
                    }else{
                      this.$message({
                        type:"error",
                        message:res.data.msg,
                        duration:1000
                      })
                    }
                  })


            }else if(res.data.code == 102){
              let url = "userBehavior/add"
              this.userBehavior.userId = this.userId
              this.userBehavior.learningTimeToday =  live.liveduration
              this.userBehavior.learningTime =  live.liveduration
              this.userBehavior.learningCourseQuantity = 1
              this.$axios.post(url,this.userBehavior)
                  .then(res=>{
                    if (res.data.code == 101){
                      this.pushPage(live)
                    }else{
                      this.$message({
                        type:"error",
                        message:res.data.msg,
                        duration:1000
                      })
                    }
                  })

            }else{
              this.$message({
                type:"error",
                message:res.data.msg,
                duration:1000
              })
            }
          })
        },
        initCoupon(){
          let url = "coupon/queryCan"
          let now = new Date
          let y = now.getFullYear()
          let m = now.getMonth() +1
          let d = now.getDate()
          let effectiveTimeEnd = y+"-"+m+"-"+d
          console.log(effectiveTimeEnd)
          this.$axios.get(url,{
            params:{
              effectiveTimeEnd: effectiveTimeEnd,
              userId:this.userId
            }
          }).then(res=>{
            if (res.data.code == 200){
              this.couponList = res.data.data
            }else{
              this.$message({
                type:"error",
                message:res.data.msg,
                duration:1000
              })
            }
          })


        },
        show(){
          this.dialogVisible = true;
          this.couponId = ''
        },
        cancle(){
          this.dialogVisible = false
          this.couponId = ''
        },
        sure(){
          let url = "couponReport/add"
          this.couponReport.userId = this.userId
          this.couponReport.couponId = this.couponId
          this.$axios.post(
              url,this.couponReport
          ).then(res=>{
            if (res.data.code == 101){
              this.$message({
                type:"success",
                message:res.data.msg,
                duration:1000
              })
              this.dialogVisible = false
            }else{
              this.$message({
                type:"error",
                message:res.data.msg,
                duration:1000
              })
            }
          })
        },
        initLive(){
          let url = "live/liveList"
          this.$axios.get(url,{
            params:{
              now:1,
              size:100
            }
          }).then(res=>{
            if (res.data.code == 200){
              this.liveList = res.data.data.list
            }else{
              this.$message({
                type:"error",
                message:res.data.msg,
                duration:1000
              })
            }

          })
        },
        initEmpTest(){
          let jsonStr = sessionStorage.getItem("user");
          console.log(jsonStr)
          if (jsonStr){
            this.user = JSON.parse(jsonStr);
            this.userId=this.user.id;
          }
        },

      },
      created() {
        this.initLive()
        this.initCoupon()
        this.initEmpTest()

      }
    }
</script>

<style scoped>
  .cards:hover{
    box-shadow: 0 0 6px rgba(35, 173, 278, 1);;
  }

</style>